<?php

namespace App\Http\Controllers\Content;

use App\Http\Controllers\Controller;
use App\Http\Requests\ContentCategoryRequest;
use App\Models\CategoryModel ;
use App\Models\ContentModel;
use Illuminate\Http\Request;

class CategoryController extends Controller
{

    public function index(CategoryModel $category)
    {
        $categorys = $category->getAllCategory();
//        dd($categorys->toArray());
//        return 'category';
        //先获取category表中的数据
//        $category = CategoryModel::get()->toArray();
//        dd($category);die;
        return view('content.category_index',compact('categorys'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(CategoryModel $category)
    {
        $models = ContentModel::get();
//        return redirect()->route('content.category.show');
        $categorys = $category->getAllCategory();
        return view('content.category_create',compact('categorys','models'));
    }


    public function store(ContentCategoryRequest $request)
    {
        //通过模型往相应数据表中添加内容
//        Category::create($request->toArray());
        (new CategoryModel())->create($request->all());
//        (new Category())-
        //前台实现页面跳转用route方法,后台使用redirect方法,但是记得要return返回
        //dd打印route('content.category.index')返回的是一个字符串http://laravel.clsss/content/category
        return redirect()->route('content.category.index');
    }


    public function show(CategoryModel $category)
    {
        //
//        dd($category);
    }


    public function edit(CategoryModel $category)
    {
        //获取所有的contentmodel模型数据
        $models = ContentModel::get();
        //获取整理过的栏目数据
        $categorys = $category->getAllCategory();
//        dd([$category->toArray(),$category->xianshi()->toArray(),$categorys->toArray()]);
//         渲染模板的同时将传递过来的参数传递过去
        return view('content.category_edit',compact('category','categorys','models'));
    }


    public function update(ContentCategoryRequest $request, CategoryModel $category)
    {
//        dd([ 'request'=>$request,'all'=> $request->all(),$request->toArray()]);die;
//        $request是要操作的数据
//        $category是要操作的模型对象,通过调用模型对象中的方法来操作数据
//            $data = $request->toArray();
            $category->update($request->all());
            return redirect(route('content.category.index'));

    }


    public function destroy(CategoryModel $category)
    {
        $category->delete();
        return redirect(route('content.category.index'));
    }
}
